<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item label="门店名称">
              <a-input v-model="queryParam.merchantName" placeholder="请输入门店的关键字" />
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item label="门店名称">
              <a-select :allowClear="true" v-model="queryParam.merchantId" placeholder="请下拉选择门店">
                <a-select-option v-for="(item, index) in storeList" :key="index" :value="item.id">{{
                  item.storeName
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="4">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('商户结算')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div> -->

    <div></div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{ x: true }" :bordered="false" rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic" />
        </template>
        <template slot="autoSettle" slot-scope="text, record">
          <a-switch checked-children="开" un-checked-children="关" :checked="text" @change="aotuSettle(text, record)" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="showSetTlementList(record)">结算</a>
          <a-divider type="vertical" />
          <a @click="showAccountModal(record)">账户变动</a>
          <a-divider type="vertical" />
          <a @click="showDetailList(record)">查看结算记录</a>
          <a-divider type="vertical" />
          <a @click="clearOpenId(record)">清除openId</a>
        </span>
      </a-table>
    </div>

    <hc-merchant-settlement-modal ref="modalForm" @ok="modalFormOk"></hc-merchant-settlement-modal>
    <set-tlement-list ref="setTlementList" @loadData="loadData"></set-tlement-list>
    <change-account ref="accountChange" @loadData="loadData"></change-account>
    <set-telement-detail-list ref="setTelementDetailList"></set-telement-detail-list>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HcMerchantSettlementModal from './modules/HcMerchantSettlementModal.vue'
import { getAction } from '@/api/manage'
import SetTlementList from './modules/SetTlementList.vue'
import ChangeAccount from './modules/ChangeAccount.vue'
import SetTelementDetailList from './modules/SetTelementDetailList.vue'

export default {
  name: 'HcMerchantSettlementList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HcMerchantSettlementModal,
    SetTlementList,
    ChangeAccount,
    SetTelementDetailList,
  },
  data() {
    return {
      description: '商户结算管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '门店名称',
          align: 'center',
          dataIndex: 'merchantName',
        },
        {
          title: 'openId',
          align: 'center',
          dataIndex: 'merchantOpenId',
          customRender: function (text) {
            return text ? text : '暂未获取'
          },
        },
        {
          title: '周三自动结算开关',
          align: 'center',
          dataIndex: 'autoSettle',
          scopedSlots: { customRender: 'autoSettle' },
        },
        {
          title: '总账户',
          align: 'center',
          dataIndex: 'settlementAmount',
          customRender: function (text) {
            return text ? text + '元' : 0
          },
        },
        {
          title: '上次结算日期',
          align: 'center',
          dataIndex: 'settlementDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        // list: '/hc/hcMerchantSettlement/list',
        list: '/hc/hcMerchantSettlement/listNew',
        delete: '/hc/hcMerchantSettlement/delete',
        clearOpenId: '/hc/hcMerchantSettlement/clearOpenId',
        changeAutoSettle: '/hc/hcMerchantSettlement/changeAutoSettle',
        deleteBatch: '/hc/hcMerchantSettlement/deleteBatch',
        exportXlsUrl: '/hc/hcMerchantSettlement/exportXls',
        importExcelUrl: 'hc/hcMerchantSettlement/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      storeList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  mounted() {
    this.getStoreList()
    //   setTimeout(() => {
    //     this.$nextTick(() => {
    //       this.dataSource.forEach((item, index) => {
    //         item.autoSettle = true
    //       })
    //       console.log(this.dataSource)
    //       this.$forceUpdate();
    //     });
    //   }, 3000)
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    // storeChange(e) {
    //   let storeName
    //   if (e) {
    //     storeName = this.storeList.filter((item) => {
    //       return item.id == e
    //     })[0].storeName
    //   } else {
    //     storeName = ''
    //   }
    //   this.queryParam.storeName = storeName

    //   this.searchQuery()
    // },
    initDictConfig() { },
    clearOpenId(val) {
      getAction(this.url.clearOpenId, { merchantId: val.merchantId }).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          let autoSettle = false
          getAction(this.url.changeAutoSettle, { merchantId: val.merchantId, autoSettle: autoSettle }).then(res => {
            if (res.success) {
              this.loadData()
            }
          })
        }
      })
    },
    aotuSettle(text, record) {
      if (record.merchantOpenId === "") {
        this.$message.warning('系统还没有获取到该商家的openId,暂时无法使用自动结算功能');
        return;
      }
      let autoSettle = !text
      getAction(this.url.changeAutoSettle, { merchantId: record.merchantId, autoSettle: autoSettle }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        }
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'type', text: '类型' })
      fieldList.push({ type: 'string', value: 'merchantId', text: '门店ID' })
      fieldList.push({ type: 'date', value: 'settlementDate', text: '结算日期' })
      fieldList.push({ type: 'number', value: 'settlementAmount', text: '结算金额' })
      fieldList.push({ type: 'string', value: 'changeRemarks', text: '变动备注' })
      fieldList.push({ type: 'string', value: 'picture', text: '图片' })
      this.superFieldList = fieldList
    },
    //获取所有门店列表
    async getStoreList() {
      let res = await getAction('hc/hcMerchant/list', { pageNo: 1, pageSize: -1 })
      console.log('res', res)
      this.storeList = res.result.records
    },
    showSetTlementList(record) {
      this.$refs.setTlementList.show(record)
    },
    showAccountModal(record) {
      this.$refs.accountChange.show(record)
    },
    showDetailList(record) {
      this.$refs.setTelementDetailList.show(record)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>